/*
 * json_body_visualizer.c
 * This file is part of gURL
 *
 * Copyright (C) 2014 - Aleksey Konovalov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "json_body_visualizer.h"

#ifdef HAVE_JSON_GLIB

#include "body_visualizer.h"
#include "json_viewer.h"

struct _JsonBodyVisualizer {
    GtkScrolledWindow parent;
    GtkWidget* viewer;
};

struct _JsonBodyVisualizerClass {
    GtkScrolledWindowClass parent;
};

static void json_body_visualizer_interface_init(BodyVisualizerInterface* iface);

G_DEFINE_TYPE_WITH_CODE(JsonBodyVisualizer, json_body_visualizer, GTK_TYPE_SCROLLED_WINDOW,
                        G_IMPLEMENT_INTERFACE(BODY_VISUALIZER_TYPE,
                                              json_body_visualizer_interface_init))

static void json_body_visualizer_change_session(BodyVisualizer* self,
                                                Session* session) {
    JsonBodyVisualizer* jbv = JSON_BODY_VISUALIZER(self);
    if(session) {
        const GString* body = session_get_response_body(session);
        json_viewer_load_from_data(JSON_VIEWER(jbv->viewer),
                                   body->str, body->len);
    } else
        json_viewer_clear(JSON_VIEWER(jbv->viewer));
}

static gboolean json_body_visualizer_check_is_compatible(BodyVisualizer* self,
                                                         Session* session) {
    JsonBodyVisualizer* jbv = JSON_BODY_VISUALIZER(self);
    if(!session)
        return FALSE;
    const GString* body = session_get_response_body(session);
    return json_viewer_load_from_data(JSON_VIEWER(jbv->viewer), body->str, body->len);
}

static void json_body_visualizer_interface_init(BodyVisualizerInterface* iface) {
    iface->change_session = json_body_visualizer_change_session;
    iface->check_is_compatible = json_body_visualizer_check_is_compatible;
    iface->name = "JSON";
}

static void json_body_visualizer_init(JsonBodyVisualizer* self) {
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(self), GTK_SHADOW_IN);
    gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(self), NULL);
    gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(self), NULL);
    g_object_set(G_OBJECT(self), "margin", 7, NULL);
    self->viewer = json_viewer_new();
    gtk_widget_show(self->viewer);
    gtk_container_add(GTK_CONTAINER(self), self->viewer);
}

static void json_body_visualizer_class_init(JsonBodyVisualizerClass* klass) {}

GtkWidget* json_body_visualizer_new() {
    return g_object_new(JSON_BODY_VISUALIZER_TYPE, NULL);
}

#endif /* HAVE_JSON_GLIB */
